package zzs.generalframework.console.core.authorization.authfunction.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.authorization.authfunction.AuthFunctionInfo;
import zzs.generalframework.common.core.authorization.authfunction.AuthFunctionPO;
import zzs.generalframework.console.core.authorization.authfunction.dao.IAuthFunctionDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.authorization.authfunction.dao.impl
 * @author zzs
 * @date 2018年1月31日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.authorization.authfunction.dao.impl.authFuctionDaoImpl")
public class AuthFunctionDaoImpl implements IAuthFunctionDao
{
	private final Logger logger = LoggerFactory.getLogger(AuthFunctionDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(AuthFunctionPO authFunctionPO)
	{
		logger.info("====插入权限功能数据开始====");
		logger.debug("====插入权限功能数据,入参:AuthFunctionPO=[{}]====", authFunctionPO);
		entityManager.persist(authFunctionPO);
		logger.debug("====插入权限功能数据,出参:boolean=[{}]====", true);
		logger.info("====插入权限功能数据结束====");
		return true;
	}

	@Override
	public boolean update(AuthFunctionPO authFunctionPO)
	{
		logger.info("====更新权限功能数据开始====");
		logger.debug("====更新权限功能数据,入参:AuthFunctionPO=[{}]====", authFunctionPO);
		entityManager.merge(authFunctionPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新权限功能数据结束====");
		return true;
	}

	@Override
	public List<AuthFunctionPO> select(AuthFunctionInfo authFunctionInfo)
	{
		logger.info("====查询权限功能数据开始====");
		logger.debug("====查询权限功能数据,入参:AuthFunctionInfo=[{}]====", authFunctionInfo);
		List<AuthFunctionPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from AuthFunctionPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(authFunctionInfo.getFunctionID()))
		{
			sqlBuffer.append(" and a.functionID=?");
			parameter.putParameterObject(authFunctionInfo.getFunctionID());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionInfo.getFunctionCode()))
		{
			sqlBuffer.append(" and a.functionCode=?");
			parameter.putParameterObject(authFunctionInfo.getFunctionCode());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionInfo.getFunctionName()))
		{
			sqlBuffer.append(" and a.functionName=?");
			parameter.putParameterObject(authFunctionInfo.getFunctionName());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionInfo.getFunctionDesc()))
		{
			sqlBuffer.append(" and a.functionDesc=?");
			parameter.putParameterObject(authFunctionInfo.getFunctionDesc());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionInfo.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(authFunctionInfo.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionInfo.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(authFunctionInfo.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(authFunctionInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询权限功能数据,出参:List<AuthFunctionPO>=[{}]====", returnList);
		logger.info("====查询权限功能数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(AuthFunctionPO authFunctionPO)
	{
		logger.info("====删除权限功能数据开始====");
		logger.debug("====删除权限功能数据,入参:AuthFunctionPO=[{}]====", authFunctionPO);
		entityManager.remove(authFunctionPO);
		logger.debug("====删除权限功能数据,出参:boolean=[{}]====", true);
		logger.info("====删除权限功能数据结束====");
		return true;
	}

	@Override
	public Long count(AuthFunctionPO authFunctionPO)
	{
		logger.info("====统计权限功能数据开始====");
		logger.debug("====统计权限功能数据,入参:AuthFunctionPO=[{}]====", authFunctionPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from AuthFunctionPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(authFunctionPO.getFunctionID()))
		{
			sqlBuffer.append(" and a.functionID=?");
			parameter.putParameterObject(authFunctionPO.getFunctionID());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionPO.getFunctionCode()))
		{
			sqlBuffer.append(" and a.functionCode=?");
			parameter.putParameterObject(authFunctionPO.getFunctionCode());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionPO.getFunctionName()))
		{
			sqlBuffer.append(" and a.functionName=?");
			parameter.putParameterObject(authFunctionPO.getFunctionName());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionPO.getFunctionDesc()))
		{
			sqlBuffer.append(" and a.functionDesc=?");
			parameter.putParameterObject(authFunctionPO.getFunctionDesc());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionPO.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(authFunctionPO.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionPO.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(authFunctionPO.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(authFunctionPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(authFunctionPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计权限功能数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计权限功能数据结束====");
		return returnLong;
	}

}
